package com.aistar.service.impl;

import com.aistar.mapper.TravellerMapper;
import com.aistar.pojo.Traveller;
import com.aistar.pojo.TravellerExample;
import com.aistar.service.CommomTravelerService;
import com.aistar.util.ServerResponse;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommonTravelerServiceImpl implements CommomTravelerService {

    @Autowired
    TravellerMapper travellerMapper;

    /**
     * 根据所属用户id获取该用户拥有的所有旅客信息
     * @param cusId
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    public ServerResponse getAllTravelersByCusId(Integer cusId) {
        //存放查询出的原始数据的集合
        List<Traveller> travellers = new ArrayList<>();
        //存放处理后的数据的集合
        List<Traveller> travellers1 = new ArrayList<>();

        TravellerExample travellerExample = new TravellerExample();
        travellerExample.createCriteria().andCustomerIdEqualTo(cusId).andStatusEqualTo("1");

        travellers = travellerMapper.selectByExample(travellerExample);

        travellers.forEach(traveller -> {
            //获取手机号 进行处理
            String telSource = traveller.getTravellerTelno();
            String tel = telSource.substring(0,3) + "****"
                    + telSource.substring(7,telSource.length());
            traveller.setTravellerTelno(tel);

            //获取身份证号 进行处理
            String idSource = traveller.getCertificateNo();
            String id = idSource.substring(0,6) + "********"
                    + idSource.substring(14,idSource.length());
            traveller.setCertificateNo(id);

            travellers1.add(traveller);
        });



        if(travellers != null && travellers.size()>0)
            return ServerResponse.getDataSuccess(travellers1);
        else
            return ServerResponse.getDataFailed(travellers1);

    }

    /**
     * 添加旅客信息
     * @param traveller
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    public ServerResponse addTraveler(Traveller traveller) {

        int rows = travellerMapper.insert(traveller);

        if(rows == 0){
            return ServerResponse.addFailed();
        }else {
            return ServerResponse.addSuccess();
        }
    }

    /**
     * 修改旅客信息
     * @param traveller
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    public ServerResponse editTraveler(Traveller traveller) {

        int rows = travellerMapper.updateByPrimaryKeySelective(traveller);
        System.out.println(rows);
        if(rows > 0){
            return ServerResponse.updateSuccess();
        }else {
            return ServerResponse.updateFailed();
        }
    }

    /**
     * 根据旅客id查询旅客信息
     * @param trvId
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    public ServerResponse getTravelerByKey(Integer trvId) {

        Traveller traveller = travellerMapper.selectByPrimaryKey(trvId);

        if (traveller != null)
            return ServerResponse.getDataSuccess(traveller);
        else
            return ServerResponse.getDataFailed(traveller);

    }

    /**
     * 将指定旅客信息删除（状态为0）
     * @param trvId
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
    public ServerResponse modifyTtavelerDel(Integer trvId) {

        Traveller traveller = travellerMapper.selectByPrimaryKey(trvId);
        traveller.setStatus("0");

        int rows = travellerMapper.updateByPrimaryKeySelective(traveller);
        System.out.println(rows);
        if(rows > 0){
            return ServerResponse.updateSuccess();
        }else {
            return ServerResponse.updateFailed();
        }
    }

    /**
     * 据旅客姓名查询旅客信息
     * @param traName
     * @return
     */
    @Override
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")

    public ServerResponse getTravelerByName(String traName) {

        List<Traveller> list = new ArrayList<>();
        TravellerExample example = new TravellerExample();
        example.createCriteria().andTravellerNameEqualTo(traName).andStatusEqualTo("1");

        list = travellerMapper.selectByExample(example);


        if(list != null && list.size()>0)
            return ServerResponse.getDataSuccess(list);
        else
            return ServerResponse.getDataFailed(list);
    }
}
